package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.PageInfo;
import com.cskaoyan.shopping.form.PageResponse;
import com.cskaoyan.shopping.service.IContentService;
import com.cskaoyan.shopping.service.IHomeService;
import com.cskaoyan.shopping.service.IProductCateService;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: duolaimall
 * @description:
 * @author: Lexis
 * @create: 2022-05-19 16:08
 **/
@RestController
@RequestMapping("shopping")
public class ShoppingController {

    @Autowired
    IHomeService homeService;

    @Autowired
    IContentService contentService;

    @Autowired
    IProductCateService productCateService;

    @Autowired
    IProductService productService;

    @RequestMapping("homepage")
    public ResponseData homepage() {
        HomePageResponse homePageResponse = homeService.homepage();
        if (homePageResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(homePageResponse.getPanelContentItemDtos());
        }
        return new ResponseUtil<>().setErrorMsg(homePageResponse.getMsg());
    }

    @RequestMapping("navigation")
    public ResponseData navigation() {
        NavListResponse navListResponse = contentService.queryNavList();
        if (navListResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(navListResponse.getPannelContentDtos());
        }
        return new ResponseUtil<>().setErrorMsg(navListResponse.getMsg());
    }

    @RequestMapping("categories")
    public ResponseData categories() {
        AllProductCateResponse allProductCateResponse = productCateService.getAllProductCate();
        if (allProductCateResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(allProductCateResponse.getProductCateDtoList());
        }
        return new ResponseUtil<>().setErrorMsg(allProductCateResponse.getMsg());
    }

    @GetMapping("recommend")
    public ResponseData recommend() {
        RecommendResponse recommendResponse = productService.getRecommendGoods();
        if (recommendResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(recommendResponse.getPanelContentItemDtos());
        }
        return new ResponseUtil<>().setErrorMsg(recommendResponse.getMsg());
    }

    @GetMapping("product/{id}")
    public ResponseData product(@PathVariable("id") String id) {
        ProductDetailRequest productDetailRequest = new ProductDetailRequest();
        productDetailRequest.setId(Long.valueOf(id));
        ProductDetailResponse detailResponse = productService.getProductDetail(productDetailRequest);
        if (detailResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(detailResponse.getProductDetailDto());
        }
        return new ResponseUtil<>().setErrorMsg(detailResponse.getMsg());
    }

    @GetMapping("goods")
    public ResponseData goods(PageInfo pageInfo) {

        AllProductRequest allProductRequest = new AllProductRequest();
        allProductRequest.setCid(pageInfo.getCid());
        allProductRequest.setPage(pageInfo.getPage());
        allProductRequest.setSort(pageInfo.getSort());
        allProductRequest.setSize(pageInfo.getSize());
        allProductRequest.setPriceGt(pageInfo.getPriceGt());
        allProductRequest.setPriceLte(pageInfo.getPriceLte());


        AllProductResponse allProductResponse = productService.getAllProduct(allProductRequest);
        PageResponse pageResponse = new PageResponse();
        pageResponse.setData(allProductResponse.getProductDtoList());
        pageResponse.setTotal(allProductResponse.getTotal());
        if (allProductResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(pageResponse);
        }
        return new ResponseUtil<>().setErrorMsg(allProductResponse.getMsg());
    }


}
